'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const c = Symbol('break visit'),
  y = Symbol('skip children'),
  u = Symbol('remove item');
function f(e, t) {
  'type' in e &&
    e.type === 'document' &&
    (e = { start: e.start, value: e.value }),
    m(Object.freeze([]), e, t);
}
f.BREAK = c;
f.SKIP = y;
f.REMOVE = u;
f.itemAtPath = (e, t) => {
  let o = e;
  for (const [i, n] of t) {
    const r = o == null ? void 0 : o[i];
    if (r && 'items' in r) o = r.items[n];
    else return;
  }
  return o;
};
f.parentCollection = (e, t) => {
  const o = f.itemAtPath(e, t.slice(0, -1)),
    i = t[t.length - 1][0],
    n = o == null ? void 0 : o[i];
  if (n && 'items' in n) return n;
  throw new Error('Parent collection not found');
};
function m(e, t, o) {
  let i = o(t, e);
  if (typeof i == 'symbol') return i;
  for (const n of ['key', 'value']) {
    const r = t[n];
    if (r && 'items' in r) {
      for (let l = 0; l < r.items.length; ++l) {
        const s = m(Object.freeze(e.concat([[n, l]])), r.items[l], o);
        if (typeof s == 'number') l = s - 1;
        else {
          if (s === c) return c;
          s === u && (r.items.splice(l, 1), (l -= 1));
        }
      }
      typeof i == 'function' && n === 'key' && (i = i(t, e));
    }
  }
  return typeof i == 'function' ? i(t, e) : i;
}
exports.visit = f;
